package hoyo.zqy.org.controller;

import hoyo.zqy.basic.util.ExcelUtils;
import hoyo.zqy.org.domain.ShopAuditLog;
import hoyo.zqy.org.dto.ShopDto;
import hoyo.zqy.org.service.IShopService;
import hoyo.zqy.org.domain.Shop;
import hoyo.zqy.org.query.ShopQuery;
import hoyo.zqy.basic.util.AjaxResult;
import hoyo.zqy.basic.util.PageList;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.prefs.BackingStoreException;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    public IShopService shopService;

    /**
     * 接口：添加或修改
     * @param shop  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.save(shop);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
        e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody Long[] ids){
        try {
                shopService.deleBatch(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除失败");
        }
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    public Shop findOne(@PathVariable("id")Long id) {
        return shopService.loadById(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<Shop> findAll(){
        return shopService.loadAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public PageList<Shop> queryPage(@RequestBody ShopQuery query) {
        return shopService.queryPage(query);
    }

    /**
     * 商家入驻
     * @return AjaxResult 响应给前端
     */
    @ApiOperation(value = "商家入驻",notes = "")
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"数据异常");
        }
    }

    /**
     * 审核通过
     * @return AjaxResult 响应给前端
     */
    @ApiOperation(value = "审核通过",notes = "")
    @PostMapping("/audit/pass")
    public AjaxResult pass(@RequestBody ShopAuditLog log){
        try {
            shopService.pass(log);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"审核通过但是没完全通过");
        }
    }

    /**
     * 审核驳回
     * @return AjaxResult 响应给前端
     */
    @ApiOperation(value = "审核驳回",notes = "")
    @PostMapping("/audit/reject")
    public AjaxResult reject(@RequestBody ShopAuditLog log){
        try {
            shopService.reject(log);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"审核驳回但是没完全驳回");
        }
    }

    /**
     * 导出数据
     * @return AjaxResult 响应给前端
     */
    @ApiOperation(value = "导出数据",notes = "")
    @GetMapping("/export")
    public void export(HttpServletResponse response){
        List<Shop> shops = shopService.loadAll();
        //调用导出方法
        ExcelUtils.exportExcel(shops, null, "店铺信息", Shop.class, "店铺信息.xlsx", response);
    }

    /**
     * 导入数据
     * @return
     */
    @ApiOperation(value = "导入数据",notes = "")
    @PostMapping("/importExcel")
    public void importExcel( MultipartFile file){
        List<Shop> shops = ExcelUtils.importExcel(file, 0, 1, Shop.class);
        shops.forEach(System.out::println);

    }

    /**
     * 店铺激活
     * @return AjaxResult 响应给前端
     */
    @GetMapping("/active/{id}")
    public String Active(@PathVariable("id") Long id) {
         shopService.Active(id);
         return "激活成功";
    }

    /**
     * 店铺数据
     * @return
     */
    @GetMapping("/echarts")
    public List<ShopDto> echarts(){
        List<ShopDto> shopDtos = shopService.echarts();
        return shopDtos;
    }
}
